package com.xiaobaibai.service.impl;

import com.alibaba.fastjson.JSON;
import com.xiaobaibai.bo.ProductEs;
import com.xiaobaibai.common.EsCommon;
import com.xiaobaibai.entity.Product;
import com.xiaobaibai.service.IEsService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class EsServiceImpl implements IEsService {

    @Autowired
    private RestHighLevelClient esClient;

    @Override
    public void addProduct(Product product) {

        ProductEs productEs=new ProductEs();
        BeanUtils.copyProperties(product,productEs);
        productEs.setProductImage(product.getProductImage().get(0));

        IndexRequest indexRequest=new IndexRequest(EsCommon.PRODUCTINDEX);
        indexRequest.id(productEs.getId().toString());
        indexRequest.source(JSON.toJSONString(productEs), XContentType.JSON);

        IndexResponse indexResponses=null;
        try {
            indexResponses = esClient.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void deleteProduct(Integer productId) {
        DeleteRequest deleteRequest=new DeleteRequest(EsCommon.PRODUCTINDEX,productId.toString());
        try {
            DeleteResponse delete = esClient.delete(deleteRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateProduct(Product product) {

        ProductEs productEs=new ProductEs();
        BeanUtils.copyProperties(product,productEs);
        productEs.setProductImage(product.getProductImage().get(0));

        UpdateRequest updateRequest=new UpdateRequest(EsCommon.PRODUCTINDEX,product.getId().toString());
        updateRequest.doc(JSON.toJSONString(productEs),XContentType.JSON);

        try {
            UpdateResponse updateResponse = esClient.update(updateRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void updateProductStatus(List<Integer> list, boolean up) {

        BulkRequest bulkRequest=new BulkRequest();

        list.forEach(l->{
            ProductEs productEs=new ProductEs();
            productEs.setId(l);
            productEs.setProductStatus(up);
            UpdateRequest updateRequest=new UpdateRequest(EsCommon.PRODUCTINDEX,l.toString());
            updateRequest.doc(JSON.toJSONString(productEs),XContentType.JSON);
            bulkRequest.add(updateRequest);
        });

        try {
            BulkResponse bulk = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
